//
//  ShapedImageView.m
//  OneCard
//
//  Created by sally on 16/2/29.
//  Copyright © 2016年 koalac. All rights reserved.
//

#import "ShapedImageView.h"

@interface ShapedImageView () {
  CALayer *_contentLayer;
  CAShapeLayer *_maskLayer;
}

@end

@implementation ShapedImageView

- (instancetype)initWithFrame:(CGRect)frame maskImage:(UIImage *)maskImage {
  self = [super initWithFrame:frame];
  if (self) {
    [self setUpWithMaskImage:maskImage];
  }
  return self;
}

- (void)setUpWithMaskImage:(UIImage *)maskImage {
  _maskLayer = [CAShapeLayer layer];
  _maskLayer.fillColor = [UIColor blackColor].CGColor;
  _maskLayer.strokeColor = [UIColor clearColor].CGColor;
  _maskLayer.frame = self.bounds;
  _maskLayer.contentsCenter = CGRectMake(0.5, 0.5, 0.1, 0.1);
  _maskLayer.contentsScale = [UIScreen mainScreen].scale;//自动拉伸的效果且不变形
  _maskLayer.contents = (id)maskImage.CGImage;
  
  _contentLayer = [CALayer layer];
  _contentLayer.mask = _maskLayer;
  _contentLayer.frame = self.bounds;
  [self.layer addSublayer:_contentLayer];
}

- (void)setImage:(UIImage *)image {
  _contentLayer.contents = (id) image.CGImage;
}

@end
